package com.jm.wf.data;

import java.sql.ResultSet;
import java.util.Date;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.utils.Utils;
import com.jm.web.data.BaseDataEntities;
import com.jm.web.data.BaseDataEntity;
import com.jm.web.data.CrudDataEntity;

public class WFInstance extends CrudDataEntity{
	public final static String WF_STATE_NOSTART 	= "0";//未启动
	public final static String WF_STATE_CHECKING 	= "1";//签核中
	public final static String WF_STATE_FINSH 		= "2";//已完成
	public final static String WF_STATE_BACK 		= "3";//退單
	public final static String WF_STATE_CANCEL 		= "4";//取消
	
	public final static String JM_STATUS_NOOK 		= "0";//不OK
	public final static String JM_STATUS_OK 		= "1";//OK
	public final static String JM_STATUS_BACK 		= "3";//退單
	public final static String JM_STATUS_CANCEL 	= "4";//取消
	
	public WFInstance(Utils utils, String dataSource) {
		super(utils, dataSource, "wf..wf003", "sn");
	}
	
	public String getKeyValue(){
		return this.gpsv("sn");
	}
	
	public String getWFState(){
		return this.gpsv("wfstate");
	}
	
	public String getCurWFStepId(){
		return this.gpsv("wf002sn");
	}
	
	public void setCurWFStepID(String wf002sn){
		this.spsv("wf002sn", wf002sn);
	}
	
	public String getCurWFStepName(){
		return this.gpsv("wfstepname");
	}
	
	public void setCurWFStepName(String wfstepname){
		this.spsv("wfstepname", wfstepname);
	}
	
	public String getWFBillNo(){
		return this.gpsv("wfbillno");
	}
	
	public String getWFdataKeyValue(){
		return this.gpsv("wfdatakeyvalue");
	}
	
	public int getVersion(){
		return this.gpiv("version");
	}
	
	public void setVersion(int version){
		this.spiv("version", version);
	}
	
	/**
	 * @return
	 * @throws Exception
	 * 刪除實例 ,及對應的操作步驟
	 */
	public int deleteSelfAndCurrentWFOPStep() throws Exception{
		StringBuilderEx sql = new StringBuilderEx();
		sql.Append(" delete from wf..wf003 where sn = '%1$s' ",this.getKeyValue());
		sql.Append(" delete from wf..wf004 where wf003sn = '%1$s' and wf002sn = '%2$s' ",this.getKeyValue(),this.getCurWFStepId());
		return this.getUtils().executeUpdate(this.getDatasource(), sql.toString());
	}
	
	/**
	 * @return
	 * @throws Exception
	 */
	public int deleteCurrentWFOPStep2() throws Exception{
		StringBuilderEx sql = new StringBuilderEx();
		sql.Append(" delete from wf..wf004 where wf003sn = '%1$s' and wf002sn = '%2$s' ",this.getKeyValue(),this.getCurWFStepId());
		return this.getUtils().executeUpdate(this.getDatasource(), sql.toString());
	}
	
	/**
	 * @param wfstepId
	 * @param wfStepName
	 * @return
	 * @throws Exception
	 * 還原流程的當前步驟
	 */
	public int restoreWFStep(String wfstepId,String wfStepName)throws Exception{
		this.setCurWFStepID(wfstepId);
		this.setCurWFStepName(wfStepName);
		return this.update("wf002sn,wfstepname");
	}
	
	/**
	 * 當前操作步驟是否都已經完成
	 */
	public boolean isCurWFOPStepAllFinsh() throws Exception{
		StringBuilderEx sql = new StringBuilderEx();
		sql.Append("select count(*) as cnt from wf..wf004 where isfinsh = 0  and isop = 1 and wf003sn = '%1$s' and wf003version = %2$s and wf002sn = '%3$s' ",
					this.getKeyValue(),
					String.valueOf(this.getVersion()),
					this.getCurWFStepId());
		ResultSet rs = this.getUtils().executeQuery(this.getDatasource(), sql.toString());
		return rs.next() && rs.getInt("cnt") ==0 ;
	}
	
//	public int setCurOPStepAllFinsh() throws Exception{
//		StringBuilderEx sql = new StringBuilderEx();
//		sql.Append("update wf..wf004 set isfinsh = 1 where isfinsh = 0 and isop = 1 and wf003sn = '%1$s' and wf003version = %2$s and wf002sn = '%3$s' ",
//					this.getKeyValue(),
//					String.valueOf(this.getVersion()),
//					this.getCurWFStepId());
//		return this.getUtils().executeUpdate(this.getDatasource(), sql.toString());
//	}
	
	/**
	 * @return
	 * @throws Exception
	 */
	public int deleteCurOpStepAllNotFinsh() throws Exception{
		StringBuilderEx sql = new StringBuilderEx();
		sql.Append("delete wf..wf004 where isfinsh = 0 and isop = 1 and wf003sn = '%1$s' and wf003version = %2$s and wf002sn = '%3$s' ",
					this.getKeyValue(),
					String.valueOf(this.getVersion()),
					this.getCurWFStepId());
		return this.getUtils().executeUpdate(this.getDatasource(), sql.toString());
	}
	
	/**
	 * @return
	 * @throws Exception
	 */
	public String getCheckRecordHtml() throws Exception{
		Utils utils = getUtils();
		StringBuffer sb = new StringBuffer();
		sb.append("<table border=\"1\"><tr><td>次数</td><td>签单步骤</td>	<td>签单人</td><td>签单动作</td><td>签单意见</td>	<td>签单时间</td></tr>");
		BaseDataEntities rows= getCheckRecordS();
		for (int i = 0; i < rows.size(); i++) {
			BaseDataEntity row = rows.get(i);
			sb.append("<tr>");
			sb.append("<td>第").append(row.gpiv("wf003version")+1).append("次</td>");
			sb.append("<td>").append(row.gpsv("wfstepname")).append("&nbsp;</td>");
			sb.append("<td>").append(row.gpsv("opchname")).append("</td>");
			sb.append("<td>").append(row.gpsv("optext")).append("</td>");
			sb.append("<td>").append(row.gpsv("oprem").replaceAll("\r\n", "<br>")).append("&nbsp;</td>");
			sb.append("<td>").append(row.gpsv("stropDay")).append("</td>");
			sb.append("</tr>");
		}
		sb.append("</table>");
		return sb.toString();
	}
	
	/**
	 * @return
	 * @throws Exception
	 */
	public BaseDataEntities getCheckRecordS() throws Exception{
		BaseDataEntities rows = new BaseDataEntities();
		Utils utils = getUtils();
		StringBuilderEx sql =  new StringBuilderEx("select wf003version,wfstepname,optext,opchname,oprem,opday from wf..wf004 where 1=1 and optext <> '' ");
		sql.Append(" and wf003sn = '%1$s'",getKeyValue());
		sql.Append(" order by wf003version,wf002sn,opday ");
		ResultSet rs = utils.executeQuery(this.getDatasource(),sql.toString());
		while(rs.next()){
			BaseDataEntity row = new BaseDataEntity();
			row.FromResultSet(rs, false);
			row.spsv("stropDay", utils.formatDateTimes((Date)rs.getObject("opDay"),"yyyy/MM/dd hh:mm").equals("1900/01/01 12:00") ? "" : utils.formatDateTimes((Date)rs.getObject("opDay"),"yyyy/MM/dd hh:mm"));
			rows.add(row);
		}
		return rows;
	}
	
	/**
	 * @return
	 * @throws Exception
	 * 取最后一層簽核人
	 */
	public String getLastCheckManForReport() throws Exception{
		StringBuffer sb = new StringBuffer();
		Utils utils = getUtils();
		ResultSet rs = utils.executeQuery(this.getDatasource(),"select opchname,opday from wf..wf004 where sn in(select max(a.sn) from wf..wf004 a,wf..wf003 b where a.wf003sn = b.sn and b.sn='"+this.getKeyValue()+"' and b.wfstate = '"+WF_STATE_FINSH+"')");
		if(rs.next()){
			sb.append(utils.getRsString(rs, "opchname")).append("(").append(utils.formatDateTimes((Date)rs.getObject("opday"), "MM/dd")).append(")");
		}
		return sb.toString();
	}
	
	/**
	 * @param wfstate
	 * @return
	 * 是否是可編輯狀態
	 */
	public static boolean isCanEditWFState(String wfstate){
		return !(WF_STATE_CHECKING.equals(wfstate) || WF_STATE_FINSH.equals(wfstate)); 
	}
}
